package com.link.base.base.project.service;

import com.link.base.base.project.dao.mybatis.mapper.ProjectCarOutputMapper;
import com.link.base.base.project.model.Project;
import com.link.base.base.project.model.ProjectCarOutput;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 年产量表
 *
 * @author 王昊
 * @date 2020-11-23 13:29:26
 */
@Service
public class ProjectCarOutputServiceImpl extends BasicServiceImpl<ProjectCarOutput> implements ProjectCarOutputService {

    private static final Logger logger = LogManager.getLogger(ProjectCarOutputServiceImpl.class);

    @Resource
    private ProjectCarOutputMapper    projectCarOutputMapper;

    @Resource
    private ProjectService projectService;

    @Override
    public BasicMapper<ProjectCarOutput> getBasicMapper() {
        return projectCarOutputMapper;
    }

    @Override
    public void beforUpsert(ProjectCarOutput entity) throws Exception {
        super.beforUpsert(entity);
        String year = entity.getYear();
        if (StringUtils.isNotBlank(year) && year.length() == 4) {
            entity.setYear(year + "-01-01 00:00:00");
        }

        ProjectCarOutput queryOne = new ProjectCarOutput();
        queryOne.setYear(entity.getYear());
        queryOne.setProjectId(entity.getProjectId());
        List<ProjectCarOutput> projectCarOutputList = projectCarOutputMapper.queryByExamplePage(queryOne);
        if (projectCarOutputList.size() == 1) {
            ProjectCarOutput record = projectCarOutputList.get(0);
            if (!record.getId().equals(entity.getId())) {
                throw new ServiceException("IYUPROJCAR-100");
            }
        } else if (projectCarOutputList.size() > 1) {
            throw new ServiceException("IYUPROJCAR-100");
        }


        Project projectEntity = new Project();
        projectEntity.setId(entity.getProjectId());
        Project project = projectService.queryById(projectEntity);
        BigDecimal zero = new BigDecimal(0);
        BigDecimal quoteCost = project.getAvgQuotaCost() == null ? zero : project.getAvgQuotaCost();
        BigDecimal num = entity.getAnnualOutput()  == null ? zero : entity.getAnnualOutput();
        BigDecimal totalNum = quoteCost.multiply(num);
        entity.setAnnualSales(totalNum);
    }
//
//    @Override
//    public ProjectCarOutput queryById(ProjectCarOutput entity) throws Exception {
//        ProjectCarOutput result = super.queryById(entity);
//        if (result == null) {
//            throw new ServiceException("IYUPROJ-100");
//        }
//        Long projectId = entity.getProjectId();
//        Project project = projectService.queryById(projectId);
//        if (project == null) {
//            throw new ServiceException("IYUPROJ-101");
//        }
//        BigDecimal zero = new BigDecimal(0);
//        BigDecimal quoteCost = project.getAvgQuotaCost() == null ? zero : project.getAvgQuotaCost();
//        BigDecimal num = result.getAnnualOutput()  == null ? zero : result.getAnnualOutput();
//        BigDecimal totalNum = quoteCost.multiply(num);
//        result.setAnnualSales(totalNum);
//        return result;
//    }

    /**
     * 根据项目头更新年产量计算
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    public List<ProjectCarOutput> queryByExamplePage(ProjectCarOutput entity) throws Exception {
        List<ProjectCarOutput> projectCarOutputs = projectCarOutputMapper.queryByExamplePage(entity);
        if (projectCarOutputs != null && !projectCarOutputs.isEmpty()) {
            Project projectEntity = new Project();
            projectEntity.setId(projectCarOutputs.get(0).getProjectId());
            Project project = projectService.queryById(projectEntity);
            for (ProjectCarOutput projectCarOutput : projectCarOutputs) {
                BigDecimal zero = new BigDecimal(0);
                BigDecimal quoteCost = project.getAvgQuotaCost() == null ? zero : project.getAvgQuotaCost();
                BigDecimal num = projectCarOutput.getAnnualOutput()  == null ? zero : projectCarOutput.getAnnualOutput();
                BigDecimal totalNum = quoteCost.multiply(num);
                projectCarOutput.setAnnualSales(totalNum);
            }
        }
        return projectCarOutputs;
    }
}